/**
 * File containing all the Controller classes
 */

/**
 * Basic Controller class
 * @type {omplusClass}
 */
Omplus.controllers.Controller = Omplus.base.extend(Omplus.base.Base,{

    /**
     * Creating Event arrays for each model or view
     */
    init: function(){},
    omplusName : null,
    omplusType : "Controller",
    /**
     * Array with containers which will be processed by this controller
     */
    views : {},
    models : {},

    /**
     * List of key-values that show which view is connected to which model
     */
    references : {},

    modelReferences : {},

    onCreateModel : function(sender, args){
        Omplus.globals.getObject(args[0]).models[sender] = true;
        if (args[1] != null){
            if(!Omplus.globals.modelReferences[args[1]]){
                Omplus.globals.modelReferences[args[1]] = []
            }
            Omplus.globals.modelReferences[args[1]].push(sender);
        }
    },

    onDestroyModel : function(sender, controller){
        Omplus.globals.getObject(controller).models[sender] = false;
    },

    onCreateView : function(sender, args){
        var helper = Omplus.globals.getObject(args[0]);
        helper.views[sender] = true;
        if(!helper.references[args[1]]){
            helper.references[args[1]] = []
        }
        helper.references[args[1]].push(sender);
    },

    onDestroyView : function(sender, controller){
        Omplus.globals.getObject(controller).views[sender] = false;
    },

    onUpdateModel : function(sender, args){
        var controller = Omplus.globals.getObject(args[0]);
        var model = Omplus.globals.getObject(sender);
        for(var i in controller.references[sender]){
            if (args[1] == null || (args[1] != null && controller.views[args[1]])){
                if (controller.references[sender][i] != args[1] && controller.views[controller.references[sender][i]]){
                    Omplus.globals.getObject(controller.references[sender][i]).setValue(model.getValue(), model.omplusName);
                    Omplus.globals.getObject(controller.references[sender][i]).emptyError();
                }
                else if (args[2] && controller.views[controller.references[sender][i]]) Omplus.globals.getObject(controller.references[sender][i]).emptyError();
            }
        }
        if (model.getSynchroniseWith() != null){
            if (model.getSynchroniseWith() != args[1])
                Omplus.globals.getObject(model.getSynchroniseWith()).setValue(model.getValue(), model.omplusName);
        }
        if (Omplus.globals.modelReferences[sender]){
            for (var i in Omplus.globals.modelReferences[sender]){
                if (Omplus.globals.modelReferences[sender][i] != args[1] && Omplus.globals.getObject(Omplus.globals.modelReferences[sender][i])){
                    Omplus.globals.getObject(Omplus.globals.modelReferences[sender][i]).setValue(model.getValue(), model.omplusName);
                }
            }
        }
    },

    onUpdateView : function(sender, controller){
        var view = Omplus.globals.getObject(sender);
        if (Omplus.globals.getObject(controller).models[view.model])
            Omplus.globals.getObject(view.model).setValue(view.getValue(), view.omplusName);
    },

    onErrorModel : function(sender, args){
        var controller = Omplus.globals.getObject(args[0]);
        if (args[1] != null && controller.views[args[1]]) {
            Omplus.globals.getObject(args[1]).setError(args[2]);
        }
        else {
            for(var i in controller.references[sender]){
                if (controller.views[controller.references[sender][i]] && Omplus.globals.getObject(controller.references[sender][i]).setError){
                    Omplus.globals.getObject(controller.references[sender][i]).setError(args[2]);
                }
            }
        }
    }
});